//根据DOM元素的id构造出一个编辑器
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    matchBrackets: true,
    mode: "text/x-groovy",
    theme: "dracula",	//设置主题
    lineWrapping: true,	//代码折叠
    foldGutter: true,
    gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
    matchBrackets: true
});
editor.setSize('auto','auto');
editor.setValue("");
layui.use(['upload','layer', 'form'], function () {
    var layer = layui.layer
        , $ = layui.$
        , form = layui.form
        , upload = layui.upload;
    //下拉框非空验证
    form.verify({
        formTextLength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255)){
                    sum=sum+1;
                } else {
                    sum=sum+2;
                }
            }
            if (sum > 200) {
                return '最多只能200个字符或者100个中文字';
            }
        }
    });

    $('.closePage').on('click', function () {
        closeSonPage();
    });

    function closeSonPage() {
        parent.layer.closeAll();
    }
    upload.render({
        elem: '#test4',
        url: '/groovy/addScript',
        accept: 'file', //普通文件
        exts: 'groovy', //只允许上传压缩文件
        auto: false,//选择文件后不自动上传
        bindAction: '#uploadConfirm',
        //上传前的回调
        before: function () {
            this.data = {
                name: $('input[name="scriptName"]').val(),
                code: editor.getValue()
            }
        },
        //选择文件后的回调
        choose: function (obj) {
            var elementById = document.getElementById("showEditContent");
            elementById.style.display="block";
            obj.preview(function (index, file, result) {
                $('input[name=scriptName]').val(file.name);
                editor.setValue(BASE64.decode(result.split('base64,')[1]));
            })
        },
        //操作成功的回调
        done: function (res, index, upload) {
            if (res.code == 200) {
                layer.msg(res.msg, { time: 900, icon: 6 }, function () {
                    window.parent.renderTable();
                    closeSonPage();
                });//失败的表情5 成功的表情6
                return;
            } else {
                layer.msg(res.msg, { icon: 5 });//失败的表情5 成功的表情6
                return;
            }
        },
        //上传错误回调
        error: function (index, upload) {
            layer.alert('上传失败！');
        }
    });
});

